<template>
  <el-dialog :title="form.teacherId ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="老师名称" prop="teacherName">
            <el-input v-model="form.teacherName" placeholder="请输入老师名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <!-- prop="phone" -->
          <el-form-item label="手机号">
            <el-input v-model="form.phone" placeholder="请输入手机号" maxlength="11" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <!-- prop="subTeacherName" -->
          <el-form-item label="名下老师">
            <el-input type="textarea" v-model="form.subTeacherName" placeholder="请输入名下老师" />
          </el-form-item>
        </el-col>

        <!-- prop="subordinateCount" -->
        <el-col :span="24" class="mb20">
          <el-form-item label="关联老师数量">
            <el-input v-model="form.subordinateCount" placeholder="请输入关联老师数量" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="AdmissionTeachersDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/admin/admissionTeachers'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  teacherId: '',
  teacherName: '',
  subTeacherName: '',
  subordinateCount: '',
});

// 定义校验规则
const dataRules = ref({
  teacherName: [{ required: true, message: '老师名称不能为空', trigger: 'blur' }],
  phone: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
  subTeacherName: [{ required: true, message: '名下老师不能为空', trigger: 'blur' }],
  subordinateCount: [{ required: true, message: '关联老师数量不能为空', trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.teacherId = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取admissionTeachers信息
  if (id) {
    form.teacherId = id
    getAdmissionTeachersData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.teacherId ? await putObj(form) : await addObj(form);
    useMessage().success(form.teacherId ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getAdmissionTeachersData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj({ teacherId: id }).then((res: any) => {
    Object.assign(form, res.data[0])
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
